﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.BLL;
using Wsitech.Model;
using Wsitech.Utility;
using System.Web.UI.HtmlControls;

public partial class Backend_TextInfoAttach : BackendPageBase
{
    protected readonly string tid = HttpContext.Current.Request["tid"] ?? "";
    protected readonly string type = HttpContext.Current.Request["type"] ?? "-1";
    /// <summary>
    /// 附件类别名称
    /// </summary>
    protected AttachType attachType
    {
        get
        {
            AttachType attachType = new AttachType();
            if (type == ((int)UtilityEnum.AttachType.Image).ToString())
            {
                attachType.TypeName = "图片";
                attachType.AddUrl = "TextInfoAttachAdd.aspx";
            }
            else if (type == ((int)UtilityEnum.AttachType.Video).ToString())
            {
                attachType.TypeName = "视频";
                attachType.AddUrl = "VideoAttachAdd.aspx";
            }
            return attachType;
        }
    }
    /// <summary>
    /// 附件类别信息
    /// </summary>
    protected class AttachType
    {
        public string TypeName { get; set; }
        public string AddUrl { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindAttach();
        }
    }

    //绑定信息对应的附件
    private void bindAttach()
    {
        repMain.DataSource = AttachInfoBLL.Instance.GetListByInfoId(tid);
        repMain.DataBind();
    }

    protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton lbtnEnable = (LinkButton)e.Item.FindControl("lbtnEnable");
        LinkButton lbtnDisable = (LinkButton)e.Item.FindControl("lbtnDisable");

        //显示和隐藏启用与禁用按钮
        AttachInfo attach = e.Item.DataItem as AttachInfo;

        //预览按钮 
        HtmlAnchor viewImage = (HtmlAnchor)e.Item.FindControl("viewImage");
        HtmlAnchor viewVideo = (HtmlAnchor)e.Item.FindControl("viewVideo");

        //编辑
        HyperLink hlEdit = (HyperLink)e.Item.FindControl("hlEdit");

        if (attach != null)
        {
            if (attach.State == (int)UtilityEnum.State.Enable)
            {
                lbtnEnable.Visible = false;
            }
            if (attach.State == (int)UtilityEnum.State.Disable)
            {
                lbtnDisable.Visible = false;
            }
            if (attach.AttachType == (int)UtilityEnum.AttachType.Image)
            {
                viewImage.Visible = true;
                hlEdit.NavigateUrl = "/Backend/ImageDescription.aspx?attachid=" + attach.AttachId;
                hlEdit.CssClass = "desc";
            }
            if (attach.AttachType == (int)UtilityEnum.AttachType.Video)
            {
                viewVideo.HRef = string.Format("/Backend/VideoPreview.aspx?attachid={0}", attach.AttachId);
                viewVideo.Visible = true;
                hlEdit.NavigateUrl = string.Format("{0}?attachid={1}", attachType.AddUrl, attach.AttachId);

                hlEdit.CssClass = "colorbox";
            }
        }
    }
    protected void repMain_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string attachid = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "Enable":
                AttachInfoBLL.Instance.ChangeState(attachid, UtilityEnum.State.Enable);
                break;
            case "Disable":
                AttachInfoBLL.Instance.ChangeState(attachid, UtilityEnum.State.Disable);
                break;
            case "Delete":
                AttachInfoBLL.Instance.DeleteAttach(attachid);
                break;
            default:
                break;
        }
        Response.Redirect(Request.Url.ToString());
    }

}